from celery import Celery
import sys
import os
from django.conf import settings

CELERY_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(CELERY_DIR, '../ihome_renting'))
os.environ['DJANGO_SETTINGS_MODULE'] = 'ihome_renting.settings'

app = Celery('ihome',
             broker='redis://127.0.0.1:6379/14',
             backend='redis://127.0.0.1:6379/15'
             )

@app.task(bind=True, name='send_sms_code')
def send_sms_code(self, mobile, sms_code):
    """发送短信"""
    from libs.yuntongxun.sms import CCP

    try:
        ccp = CCP()
        result = ccp.send_template_sms(mobile, [sms_code, 5], 1)
    except:
        result = -1

    if result == -1:
        self.retry(countdown=3, max_retries=5)
